import { BaseWindow } from "../../../component/window/BaseWindow";
import { GTextFormat } from "../../../common/GTextFormat";
import { FontUtil } from "../../../common/font/FontUtil";
import { Language } from "../../../../extend/language/Language";
import { ObjCreate } from "../common/ObjCreate";
import { ImagesConst } from "../../resource/ImagesConst";
import { SWFInfo } from "../../../../com/gengine/resource/info/SWFInfo";
import { PetModelPanel } from "./PetModelPanel";
import { PetSkillItem } from "./skill/PetSkillItem";
import { SPetInfo } from "../../../../Message/Game/SPetInfo";
import { PetUtil } from "./info/PetUtil";
import { HTMLUtil } from "../../../../com/gengine/utils/HTMLUtil";
import { PetColor } from "./info/PetColor";
import { Cache } from "../../cache/Cache";
import { GuideArrowManager } from "../../manager/GuideArrowManager";
import { EffectManager } from "../../manager/EffectManager";
import { FilterConst } from "../../../component/gconst/FilterConst";
import { GButton } from "../../../../com/mui/controls/GButton";
import { MsgManager } from "../../manager/MsgManager";
import { Alert } from "../../../../com/mui/controls/Alert";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { EventName } from "../../mvc/EventName";
import { EPlayerItemPosType } from "../../../../Message/Public/EPlayerItemPosType";
import { PetIlluEventName } from "./illu/PetIlluEventName";
import { SPetSkill } from "../../../../Message/Game/SPetSkill";
import { _EPlayerItemPosTypePet } from "../../../../EPlayerItemPosType/_EPlayerItemPosTypePet";
type int = number;
//class PetAddWindow
    
    export  class PetAddWindow extends BaseWindow
    {
        constructor()
        {
            super();
            this._labelTft = new GTextFormat(FontUtil.songtiName, 16, 6350622);
            this._valueTft = new GTextFormat(FontUtil.songtiName, 16, 15788607);
            if (PetAddWindow._instance) 
            {
                throw new Error(" PetAddWindow 单例 ");
            }
            this.setSize(325, 285);
            titleHeight = 30;
            this.title = Language.getString(54175);
            return;
        }

        protected /* override */ createChildren(): void
        {
            var loc1;
            super.createChildren();
            ObjCreate.createBg(17, 40, 290, 200, this);
            ObjCreate.createBg(190, 42, 120, 25, this, ImagesConst.TileBg);
            this._bgSprite = new Object /* flash.display.Sprite */();
            this._bgSprite.x = 19;
            this._bgSprite.y = 42;
            this.addChild(this._bgSprite);
            this._bg = new Object /* flash.display.Bitmap */();
            this._bgSprite.addChild(this._bg);
            with ({}) 
            {
            };
            "PetModelBg.swf".load(
#error('pushwith') {}
            , 
#error('pushwith') {}
            .onLoaded = (arg1: SWFInfo): void
            {
                _bg.bitmapData = arg1.bitmapData;
                _bg.width = 172;
                _bg.height = 191;
                return;
            })
            this._petModel = new PetModelPanel();
            this._petModel.isShowTurnBtn = false;
            this._petModel.x = 110;
            this._petModel.y = 190;
            this.addChild(this._petModel);
            this._nameTxt = ObjCreate.createGTextField("", 190, 45, 115, 20, this);
            this._nameTxt.autoSize = flash.text.TextFieldAutoSize.CENTER;
            ObjCreate.createGTextField(Language.getString(54176), 200, 70, 70, 20, this, this._labelTft);
            this._talentTxt = ObjCreate.createGTextField("", 250, 70, 100, 30, this, this._valueTft);
            ObjCreate.createGTextField(Language.getString(54177), 200, 100, 70, 20, this, this._labelTft);
            this._growthTxt = ObjCreate.createGTextField("", 250, 100, 100, 30, this, this._valueTft);
            ObjCreate.createGTextField(Language.getString(54178), 200, 130, 110, 20, this, this._labelTft);
            this._spiritTxt = ObjCreate.createGTextField("", 250, 130, 100, 30, this, this._valueTft);
            ObjCreate.createGTextField(Language.getString(54179), 200, 160, 100, 30, this, this._labelTft);
            ObjCreate.createBg(220, 180, 38, 38, this, ImagesConst.PackItemBg);
            this._skillItem = new PetSkillItem();
            this._skillItem.setSize(32, 32);
            this._skillItem.move(222, 182);
            this.addChild(this._skillItem);
            this._getBtn = ObjCreate.createGButton(Language.getString(54180), 82, 250, 60, 24, this);
            this._abandonBtn = ObjCreate.createGButton(Language.getString(54181), 165, 250, 60, 24, this);
            this._abandonBtn.name = "abandon";
            this._abandonBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onAbandonBtnClickHandler);
            this._getBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onAbandonBtnClickHandler);
            return;
        }

        public updateByPetInfo(arg1: SPetInfo): void
        {
            this._petInfo = arg1;
            if (arg1) 
            {
                this._petModel.updateModelByCode(arg1.code);
                this._nameTxt.htmlText = PetUtil.getPetName(arg1);
                this._talentTxt.htmlText = HTMLUtil.addColor(arg1.talent.toString(), PetColor.getTalentColorByTalent(1, arg1.talent).color);
                this._growthTxt.text = arg1.growth.toString();
                this._spiritTxt.text = arg1.spirit.toString();
                if (arg1.skills.length > 0) 
                {
                    this._petSkill = arg1.skills[0];
                    this._skillItem.skill = this._petSkill;
                }
                else 
                {
                    this._skillItem.skill = null;
                }
            }
            if (Cache.instance.guide.guidePetAdd) 
            {
                this.guidePetAdd();
            }
            return;
        }

        /* internal  */guidePetAdd(): void
        {
            var loc1=null;
            loc1 = GuideArrowManager.getGuideArrow(GuideArrowManager.Dir_L, null, 0);
            loc1.x = this._getBtn.x + this._getBtn.width + 10;
            loc1.y = this._getBtn.y + this._getBtn.height / 2;
            $addChild(loc1);
            EffectManager.glowFilterReg(this._getBtn, [FilterConst.guideTipsFilter]);
            return;
        }

        /* internal  */hidePetAdd(): void
        {
            if (Cache.instance.guide.guidePetAdd) 
            {
                if (GuideArrowManager.guideArrow.parent == this) 
                {
                    GuideArrowManager.hideGuideArrow();
                }
                EffectManager.glowFilterUnReg(this._getBtn);
            }
            return;
        }

        /* internal  */onAbandonBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var e: Object /* flash.events.MouseEvent */;
            var name: string;
            var onBtnClickHandler: Function;
            var pet: SPetInfo;

            var loc1;
            pet = null;
            e = arg1;
            name = (e.target as GButton).name;
            if (this._petInfo) 
            {
                if (name != "abandon") 
                {
                    pet = Cache.instance.pet.getPetInfoByUid(this._petInfo.uid);
                    if (pet) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(54183));
                    }
                    else 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(54184));
                    }
                    this.hide();
                }
                else if (PetColor.isAbovePurple(this._petInfo.talent)) 
                {
                    onBtnClickHandler = (arg1: int): void
                    {
                        if (arg1 == Alert.OK) 
                        {
                            Dispatcher.dispatchEvent(new DataEvent(EventName.PetRelease, {"uid": _petInfo.uid, "posType": _EPlayerItemPosTypePet}));
                            PetAddWindow.instance.hide();
                            Alert.resetToDefault();
                        }
                        return;
                    }
                    Alert.show(HTMLUtil.addColor(Language.getString(54182), "#ff0000"), null, Alert.OK | Alert.CANCEL, null, onBtnClickHandler);
                }
                else 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.PetRelease, {"uid": this._petInfo.uid, "posType": _EPlayerItemPosTypePet}));
                    this.hide();
                }
            }
            return;
        }

        public /* override */ show(arg1: int=0, arg2: int=0): void
        {
            super.show(arg1, arg2);
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetAddWindowShow));
            return;
        }

        public /* override */ hide(): void
        {
            this.hidePetAdd();
            super.hide();
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetAddWindowClose));
            Dispatcher.dispatchEvent(new DataEvent(PetIlluEventName.DealGetNewPet, this._petInfo));
            return;
        }

        public static get instance(): PetAddWindow
        {
            if (PetAddWindow._instance == null) 
            {
                PetAddWindow._instance = new PetAddWindow();
            }
            return PetAddWindow._instance;
        }

        /* internal */ /* var */_bgSprite: Object /* flash.display.Sprite */;

        /* internal */ /* var */_bg: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_petModel: PetModelPanel;

        /* internal */ /* var */_nameTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_talentTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_growthTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_spiritTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_skillItem: PetSkillItem;

        /* internal */ /* var */_getBtn: GButton;

        /* internal */ /* var */_abandonBtn: GButton;

        /* internal */ /* var */_petInfo: SPetInfo;

        /* internal */ /* var */_petSkill: SPetSkill;

        /* internal */ /* var */_labelTft: Object /* flash.text.TextFormat */;

        /* internal */ /* var */_valueTft: Object /* flash.text.TextFormat */;

        /* internal */ static /* var */_instance: PetAddWindow;
    }
